package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.system.entity.Address;
import cn.stylefeng.guns.modular.system.entity.DealerLevel;
import cn.stylefeng.guns.modular.system.entity.Level;
import cn.stylefeng.guns.modular.system.mapper.DealerLevelMapper;
import cn.stylefeng.guns.modular.system.model.params.DealerLevelParam;
import cn.stylefeng.guns.modular.system.service.AddressService;
import cn.stylefeng.guns.modular.system.service.DealerLevelService;
import cn.stylefeng.guns.modular.system.service.LevelService;
import cn.stylefeng.guns.sys.modular.system.entity.Dept;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.mapper.DeptMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.UserMapper;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;


/**
 * 经销商等级表控制器
 *
 * @author 小飞
 * @Date 2019-06-27 15:17:01
 */
@Controller
@RequestMapping("/dealerLevel")
public class DealerLevelController extends BaseController {

    private String PREFIX = "/dealerLevel";

    @Autowired
    private DealerLevelService dealerLevelService;

    @Resource
    private UserMapper userMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private LevelService levelService;

    @Autowired
    private AddressService addressService;

    @Resource
    private DeptMapper deptMapper;

    /**
     * 跳转到主页面
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/dealerLevel.html";
    }

    /**
     * 新增页面
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/add")
    public String add() {
        List<Level> levels = levelService.list();
        super.setAttr("levels",levels);
        return PREFIX + "/dealerLevel_add.html";
    }

    /**
     * 编辑页面
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/dealerLevel_edit.html";
    }

    /**
     * 新增接口
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(DealerLevelParam dealerLevelParam) {
        dealerLevelParam.setBalance(new BigDecimal(0.0));
        DealerLevel dealerLevel = dealerLevelService.getOne(new QueryWrapper<DealerLevel>()
        .lambda().eq(DealerLevel::getSysUserId,dealerLevelParam.getSysUserId()));
        if (ToolUtil.isNotEmpty(dealerLevel)){
            return  ResponseData.error(201,"已有该经销商等级，不可重复添加！");
        }else {
            this.dealerLevelService.add(dealerLevelParam);
            return ResponseData.success();
        }
    }

    /**
     * 编辑接口
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(DealerLevelParam dealerLevelParam) {
        this.dealerLevelService.update(dealerLevelParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(DealerLevelParam dealerLevelParam) {
        this.dealerLevelService.delete(dealerLevelParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(DealerLevelParam dealerLevelParam) {
        DealerLevel detail = this.dealerLevelService.getById(dealerLevelParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(DealerLevelParam dealerLevelParam) {

        return this.dealerLevelService.findPageBySpec(dealerLevelParam);
    }


}


